<?php
/**
 * @Created on 2021/05/25
 * @Author Qi
*/
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Ader extends Base{

    //广告商管理
    public function index(){

        $where = array();
        $name=request()->param('name');
        if($name){
            $where['ader'] = array('like',"%{$name}%");
        }
        $start=request()->param('start');
        if($start){
            $where['add_time'] = array('egt',strtotime($start));
        }
        $end=request()->param('end');
        if($end){
            $where['add_time'] = array('elt',strtotime($end));
        }
        if(!empty($start) && !empty($end)){
            $where['add_time'] = array('between time',["{$start}","{$end}"]);
        }
        $list=db('ader')->where($where)->order('id desc')->paginate(50,false,['query'=>request()->param()]);
        $this->assign('list',$list);
        $this->assign('name',$name);
        $this->assign('start',$start);
        $this->assign('end',$end);
        return $this->fetch();
    }

    //添加
    public function add(){

        if(request()->isPost()){
            $ader = trim(request()->param('title'));
            $data['ader'] = $ader;
            $data['add_time'] = time();
            $data['add_user'] = session('username');
            $true=db('ader')->insert($data);
            if($true){
                system_log('ader','添加广告商');
                return json(['status'=>1,'message'=>'添加成功']);
            }else{
                return json(['status'=>0,'message'=>'添加失败']);
            }
        }else{
            return $this->fetch();
        }
    }

    //修改
    public function edit(){
        if(request()->isPost()){
            $id = request()->param('id');
            $ader = trim(request()->param('title'));
            $data['ader'] = $ader;
            $true=db('ader')->where('id',$id)->update($data);
            if($true){
                system_log('ader','修改广告商');
                return json(['status'=>1,'message'=>'修改成功']);
            }else{
                return json(['status'=>0,'message'=>'修改失败']);
            }
        }else{
            $id=request()->param('id');
            $info=db('ader')->where('id',$id)->find();
            $this->assign('info',$info);
            return $this->fetch();
        }
    }

    //删除
    public function del(){
        $id=request()->param('id');
        $true=db('ader')->where('id',$id)->delete();
        if($true){
            system_log('ader','删除广告商');
            return json(['status'=>1,'message'=>'删除成功']);
        }else{
            return json(['status'=>0,'message'=>'删除失败']);
        }
    }

}